<?php
$auth = $_SESSION['auth'];
if($auth<90)
{
	echo '<center>Non hai l\'autorizzazione per accedere a questa pagina.<br><br><img src="images/accesso_negato.gif"></center>';
	exit();
}
//connessione al database
include 'connect.php';
?>


<div id="wrapper">
    	<div id="secWrapper">
        	<div id="container" class="clearfix">
            	<div id="mainCol" class="clearfix">
				    
		<h3 id="why">Gestione Farmacia - Modifica Farmaco</h3><br>
                   
		<font size="2">
		
	<table width="90%" border="1" cellpadding="5" cellspacing="0">
	<tr><th style='padding:5px' align='center'>Descrizione:</th><th style='padding:5px' align='center'>Quantita'</th><th style='padding:5px' align='center'>Scadenza</th><th style='padding:5px' align='center'>Note</th><th></th></tr>
		<?php
		$farmaci = mysql_query("select * from farmacia order by descrizione");
		while($farmaci2 = mysql_fetch_array($farmaci))
		{?>
		<tr>
			<td style='padding:5px' align='center'><?php echo $farmaci2['descrizione']; ?></td>
			<td style='padding:5px' align='center'><?php echo $farmaci2['quantita']; ?></td>
			<td style='padding:5px' align='center'><?php  if($farmaci2['scadenza'] != "0000-00-00") echo date("d-m-Y", strtotime ($farmaci2['scadenza'])); ?></td>
			<td style='padding:5px' align='center'><?php echo $farmaci2['note']; ?></td>
			<td style='padding:5px' align='center'><a href="javascript:validateForm(<?php echo$farmaci2['id']; ?>);">modifica</a></td>
		</tr>
		<?php
		}
		?>
	</table>
	
	<?php
	$farmaci = mysql_query("select * from farmacia");
	while($farmaci2 = mysql_fetch_array($farmaci))
	{
	list($anno, $mese, $giorno) = explode("-", $farmaci2['scadenza']);
	?>
	<div id="<?php echo$farmaci2['id']; ?>" style="display:none;">
			<form name="modifica<?php echo$farmaci2['id']; ?>" method="post">
			<b>Nuovi valori:</b><br>
			<table border="0">
			<tr>
				<td>Descrizione:</td><td><input value="<?php echo$farmaci2['descrizione']; ?>" type="text" size="40" name="descrizione"></td>
			</tr>
			<tr>
				<td>Quantita':</td><td><input value="<?php echo$farmaci2['quantita']; ?>" type="text" size="3" name="quantita"></td>
			</tr>
			<tr>
			<td>Scadenza:</td>
			<td>
				<select name="giorno"><option value="<?php echo $giorno; ?>"><?php echo $giorno; ?></option><?php for($i=1; $i<=31; $i++) { ?> <option value="<?php echo $i ?>"><?php echo $i; ?> </option> <?php } ?></select>
				<select name="mese"><option value="<?php echo $mese; ?>"><?php echo $mese; ?></option><?php for($i=1; $i<=12; $i++) { ?> <option value="<?php echo $i ?>"><?php echo $i; ?> </option> <?php } ?></select>
				<select name="anno">
					<option value="<?php echo $anno; ?>"><?php echo $anno; ?></option>
					<option value="<?php echo date(Y); ?>"><?php echo date(Y); ?></option>
					<option value="<?php echo date(Y)+1; ?>"><?php echo date(Y)+1; ?></option>
					<option value="<?php echo date(Y)+2; ?>"><?php echo date(Y)+2; ?></option>
					<option value="<?php echo date(Y)+3; ?>"><?php echo date(Y)+3; ?></option>
					<option value="<?php echo date(Y)+4; ?>"><?php echo date(Y)+4; ?></option>
				</select> 
			</td>
			</tr>
			<tr>
				<td>Note:</td><td><input value="<?php echo$farmaci2['note']; ?>" type="text" size="30" name="note"></td>
			</tr>
			</table>
			<input type="button" value="Modifica" onClick="validateForm2(<?php echo$farmaci2['id']; ?>);">
			</form>
			<br>
	</div>
	<?php
	}
	?>
	
	<a href="home.php?page=farmacia">Indietro</a>
	
		</font>
                </div>

<script language="Javascript">
function validateForm(id) 
	{
		document.getElementById(id).style.display='block';
	}

function validateForm2(id) 
	{
		
		//acquisisco il valore delle variabili
		var descrizione = document.forms["modifica"+id].descrizione.value;
		var quantita = document.forms["modifica"+id].quantita.value;
	    
		//controllo coerenza 
		if (descrizione == "") 
		{
		   alert("Il campo descrizione non puo' essere vuoto");
		   document.forms["modifica"+id].descrizione.focus();
		  //return false;
		}
		else if (quantita == "") 
		{
		   alert("Il campo quantita' non puo' essere vuoto");
		   document.forms["modifica"+id].quantita.focus();
		  //return false;
		}
		//mando i dati alla pagina
		else 
		{
		   document.forms["modifica"+id].action = "modificafarmaco2.php?id="+id;
		   document.forms["modifica"+id].submit();
		}
	}
</script>
                   